<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增字典' : '编辑字典'"
    :visible.sync="visible"
    width="600px"
    @closed="reset"
  >
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="80px" style="margin: 0 50px;">
      <el-form-item label="功能属性:" prop="keyFun">
        <el-select v-model="temp.keyFun" :disabled="type === 'edit'" style="width: 100%;" placeholder="请选择" clearable @change="keyFunChange">
          <el-option
            v-for="item in keyFunMap"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="属性类别:" prop="keyAttr">
        <el-select v-model="temp.keyAttr" :disabled="type === 'edit'" style="width: 100%;" placeholder="请选择" clearable>
          <el-option
            v-for="item in keyAttrList"
            :key="item.split(':')[0]"
            :label="item.split(':')[1]"
            :value="item.split(':')[0]"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态:" prop="keyStatus">
        <el-checkbox v-model="temp.keyStatus">启用</el-checkbox>
      </el-form-item>
      <el-form-item label="字典名称:" prop="keyName">
        <el-input v-model="temp.keyName" type="textarea" :rows="2" maxlength="100" show-word-limit />
      </el-form-item>
      <el-form-item label="备注:" prop="remark">
        <el-input v-model="temp.remark" type="textarea" :rows="3" maxlength="254" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { customizeDicAdd } from '@/api'

  export default {
    name: 'add-customize',
    props: {
      keyFunMap: {
        type: Array,
        required: true
      }
    },
    data() {
      return {
        visible: false,
        type: 'add',
        keyAttrList: [],
        row: {},
        temp: {
          keyFun: '',
          keyAttr: '',
          keyStatus: false,
          keyName: '',
          remark: ''
        },
        rules: {
          keyFun: [{ required: true, message: '请选择功能属性', trigger: 'change' }],
          keyAttr: [{ required: true, message: '请选择属性类别', trigger: 'change' }],
          keyName: [{ required: true, message: '请输入字典名称', trigger: 'change' }]
        }
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.row = temp
            this.temp = Object.assign(this.temp, temp, {
              keyStatus: temp.keyStatus === 2
            })
            const match = this.keyFunMap.find(item => item.value === temp.keyFun)
            if (match) {
              const tagArr = match.tag.split(',')
              const item = tagArr.find(it => Number(it.split(':')[0]) === Number(temp.keyAttr))
              this.temp.keyAttr = item.split(':')[1]
            }
            this.type = 'edit'
          }
        })
      },
      keyFunChange(value) {
        this.$set(this.temp, 'keyAttr', '')
        this.keyAttrList = []
        const match = this.keyFunMap.find(item => item.value === value)
        if (match) this.keyAttrList = match.tag.split(',')
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          const params = {
            keyFun: this.temp.keyFun,
            keyAttr: this.type === 'add' ? this.temp.keyAttr : this.row.keyAttr,
            keyName: this.temp.keyName,
            keyStatus: this.temp.keyStatus ? 2 : 1,
            remark: this.temp.remark
          }
          if (this.temp.keyId && this.temp.keyId > 0) params.keyId = this.temp.keyId
          if (!valid) return
          if (this.type === 'add') {
            this.add(params)
          } else {
            this.edit(params)
          }
        })
      },
      async add(params) {
        await customizeDicAdd(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '添加字典成功',
          type: 'success',
          duration: 2000
        })
      },
      async edit(params) {
        await customizeDicAdd(params)
        this.resolve(this.temp)
        this.visible = false
        this.$notify({
          title: '提示',
          message: '修改字典成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.type = 'add'
        this.temp = {
          keyFun: '',
          keyAttr: '',
          keyStatus: false,
          keyName: '',
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.autocomplete {
  display: flex;
  align-items: center;
  justify-content: space-between;
  span:nth-of-type(2) {
    color: #b4b4b4;
  }
}
</style>
